package com.zg.ktcomwan.ext

import android.app.AlertDialog
import android.content.Context
import android.util.Log
import com.zg.ktcomwan.base.log
import kotlinx.coroutines.CompletionHandlerException
import kotlinx.coroutines.suspendCancellableCoroutine

/***
 * @Author:      zxd
 * @CreateDate:  2021/6/30 15:27
 * @Description:
 */

suspend fun Context.alert(title: String, message: String): Boolean = suspendCancellableCoroutine {
        continuation ->
    AlertDialog.Builder(this)
        .setNegativeButton("No") { dialog, _ ->
            dialog.dismiss()
            continuation.resume(false){
                log("no")
            }
        }.setPositiveButton("Yes"){ dialog, _ ->
            dialog.dismiss()
            continuation.resume(true){
                log("yes")
            }
        }.setTitle(title).setMessage(message)
        .setOnCancelListener {
            continuation.resume(false){
                log("cancel")
            }
        }.create()
        .also {dialog ->
            continuation.invokeOnCancellation {
                dialog.dismiss()
            }
        }.show()
}